package weaver.interfaces.htsc.hrm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.doc.detail.util.DocDownloadCheckUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import sun.misc.BASE64Decoder;
import weaver.conn.RecordSet;
import weaver.docs.docs.ImageFileIdUpdate;
import weaver.general.BaseBean;
import weaver.general.GCONST;
import weaver.general.Util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

public class OrganizationSynUtil {

    /**
     * 根据分部编码查询分部ID
     * @param subcompanyCode
     * @return deptId
     */
    public String getSubcomIdByCode(String subcompanyCode){

        String subcomId = "";
        if(subcompanyCode!=null && !"".equals(subcompanyCode)){
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmsubcompany where subcompanycode=? ",new Object[]{subcompanyCode});
            if(rs.next()){
                subcomId = Util.null2String(rs.getString("id"));
            }
        }
        return subcomId;
    }


    /**
     * 部门类型转换
     * @param departmentType
     * @return
     */
    public String getDepartmentType(String departmentType){
        String deptType = "";
        if("BASE_DEPT".equals(departmentType)){
            deptType = "0";
        }else if("BASE_SUB_DEPT".equals(departmentType)){
            deptType = "1";
        }else if("SUB_DEPT".equals(departmentType)){
            deptType = "2";
        }else if("BRANCH_DEPT".equals(departmentType)){
            deptType = "3";
        }else if("BRANCH_DEPT_BUS".equals(departmentType)){
            deptType = "4";
        }else if("SALSE_DEPT".equals(departmentType)){
            deptType = "5";
        }else{
            deptType = "6";
        }
        return deptType;
    }


    /**
     * 根据部门ID获取自定义信息ID
     * @param deptId
     * @return
     */
    public String getDeptDefineidByDeptid(String deptId){
        String deptDefineId = "";
        if(deptId!=null && !"".equals(deptId)){
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmdepartmentdefined where deptid=? ",new Object[]{deptId});
            if(rs.next()){
                deptDefineId = Util.null2String(rs.getString("id"));
            }
        }
        return deptDefineId;
    }


    /**
     * 获取用户自定义数据的ID
     * @param userId
     * @param scopeid
     * @return
     */
    public int getCusFielddataId(String userId,int scopeid){
        int cusFielddataId = -1;
        if(!"".equals(userId)){
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select seqorder from cus_fielddata where scopeid=? and id=? ",new Object[]{scopeid,userId} );
            if(rs.next()){
                cusFielddataId = rs.getInt("seqorder");
            }
        }
        return cusFielddataId;
    }


    /**
     * 根据人员编号JSON数组查询对应的ID
     * @param userCodeArray
     * @return
     */
    public String getUserIdsByCodes(JSONArray userCodeArray){
        String userIds = "";
        if(userCodeArray!=null && userCodeArray.size()>0){
            for(int i=0;i<userCodeArray.size();i++)
            {
                String userId = getUserIdByCode(userCodeArray.getString(i));
                if(!"".equals(userId))
                {
                    userIds += userIds== "" ? userId : ","+userId;
                }
            }
        }
        return userIds;
    }


    /**
     * 根据JOSN数组对象中的人员编号查询对应的ID
     * @param jsonObjArray
     * @return
     */
    public String getUserIdsByCodesObj(JSONArray jsonObjArray){
        String userIds = "";
        BaseBean bb = new BaseBean();
        if(jsonObjArray!=null && jsonObjArray.size()>0){
            for(int i=0;i<jsonObjArray.size();i++){
                String empCode = jsonObjArray.getJSONObject(i).getString("empCode");
                bb.writeLog("getUserIdsByCodesObj--empcode:"+empCode);
                if(!"".equals(empCode))
                {
                    String userId = getUserIdByCode(empCode);
                    bb.writeLog("getUserIdsByCodesObj--userId:"+userId);
                    if(!"".equals(userId)){
                        userIds += userIds== "" ? userId : ","+userId;
                    }
                }
            }
        }
        return userIds;
    }


    /**
     * 根据岗位编码和部门ID获取岗位ID
     * @param jobTitleCode
     * @param deptId
     * @return jobTitlecId
     */
    public String getJobTitleIdByCode(String jobTitleCode,String deptId){
        String jobTitlecId = "";
        RecordSet rs = new RecordSet();
        if(!"".equals(jobTitleCode) && !"".equals(deptId)){
            rs.executeQuery(" select id from hrmjobtitles where jobtitlecode=? and jobdepartmentid=? ",new Object[]{jobTitleCode,deptId});
            if(rs.next()){
                jobTitlecId = Util.null2String(rs.getString("id"));
            }
        }
        return jobTitlecId;
    }

    /**
     * 根据部门编码查询部门ID
     * @param deptcode
     * @return deptId
     */
    public String getDeptIdByCode(String deptcode){
        String deptId = "";
        if(deptcode!=null && !"".equals(deptcode))
        {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmdepartment where departmentcode=? ",new Object[]{deptcode});
            if(rs.next()){
                deptId = Util.null2String(rs.getString("id"));
            }
        }
        return deptId;
    }




    /**
     * 根据JOSN数组对象中的人员编号查询对应的ID
     * @param jsonObjArray
     * @return
     */
    public String getCohead(JSONArray jsonObjArray)
    {
        RecordSet rs = new RecordSet();
        String userIds = "";
        if(jsonObjArray!=null && jsonObjArray.size()>0){
            for(int i=0;i<jsonObjArray.size();i++)
            {
                String empCode = jsonObjArray.getJSONObject(i).getString("empCode");
                String deptCode = jsonObjArray.getJSONObject(i).getString("deptCode");
                String positionCode = jsonObjArray.getJSONObject(i).getString("positionCode");
                String directorType = jsonObjArray.getJSONObject(i).getString("directorType");
                if("1".equals(directorType)){
                    String textfield2 = deptCode+";"+positionCode+";"+empCode;
                    rs.executeQuery(" select id from hrmresource where textfield2 =? ",new Object[]{textfield2});
                    if(rs.next())
                    {
                        String userId = Util.null2String(rs.getString("id"));
                        if(!"".equals(userId))
                        {
                            userIds += userIds== "" ? userId : ","+userId;
                        }
                    }
                }
            }
        }
        return userIds;
    }


    /**
     * 据人员编号查询对应的ID
     * @param userCode
     * @return userId
     */
    public String getUserIdByCode(String userCode){
        String userId = "";
        if(userCode!=null && !"".equals(userCode)){
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmresource where workcode=? and belongto is null ",new Object[]{userCode});
            if(rs.next()){
                userId = Util.null2String(rs.getString("id"));
            }
        }
        return userId;
    }


    /****
     *
     * @param jsonObjArray
     * @return
     */
    public String getUserIdsByCodes2Dept(JSONArray jsonObjArray)
    {
        String userIds = "";
        RecordSet rs = new RecordSet();
        if(jsonObjArray!=null && jsonObjArray.size()>0)
        {
            for(int i=0;i<jsonObjArray.size();i++)
            {
                String empCode = jsonObjArray.getJSONObject(i).getString("empCode");
                String deptCode = jsonObjArray.getJSONObject(i).getString("deptCode");
                String positionCode = jsonObjArray.getJSONObject(i).getString("positionCode");
                String textfield2 = deptCode+";"+positionCode+";"+empCode;
                rs.writeLog("getUserIdsByCodes2Dept-textfield2:"+textfield2);
                rs.executeQuery(" select id from hrmresource where textfield2 =? ",new Object[]{textfield2});
                int flag = 0;
                if(rs.next()){
                    String userId = Util.null2String(rs.getString("id"));
                    if(!"".equals(userId))
                    {
                        userIds += userIds== "" ? userId : ","+userId;
                    }
                    flag++;
                }

                if(flag==0)
                {
                    if(!"".equals(empCode))
                    {
                        String sql = " select id from hrmresource where workcode='"+empCode+"' and belongto is null " ;
                        rs.execute(sql);
                        if(rs.next()){
                            String userId = Util.null2String(rs.getString("id"));
                            if(!"".equals(userId))
                            {
                                userIds += userIds== "" ? userId : ","+userId;
                            }
                        }
                    }
                }
            }
        }
        return userIds;
    }






    /***
     *
     * @param url
     * @param params
     * @param headsMap
     * @return
     */
    public String sendPost(String url, String params, Map<String,String> headsMap)
    {
        BaseBean bb = new BaseBean();
        String body = "";
        String encoding = "UTF-8";
        //创建httpclient对象
        CloseableHttpClient client = HttpClients.createDefault();
        //创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);


        //设置参数到请求对象中
        StringEntity strEntity = null;//解决中文乱码问题
        try {
            strEntity = new StringEntity(params,encoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
        strEntity.setContentEncoding(encoding);
        strEntity.setContentType("application/json");
        httpPost.setEntity(strEntity);

        //设置header信息
        //指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/json");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        if (headsMap != null && !headsMap.isEmpty()) {
            headsMap.forEach((key, value) -> {
                bb.writeLog("key:"+key+" value:"+value);
                httpPost.addHeader(key, value);
            });
        }
        //执行请求操作，并拿到结果（同步阻塞）
        CloseableHttpResponse response = null;
        try {
            response = client.execute(httpPost);
            //获取结果实体
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                //按指定编码转换结果实体为String类型
                body = EntityUtils.toString(entity, encoding);
            }
           // EntityUtils.consume(entity);
            //释放链接
            response.close();
        } catch (IOException e) {
            bb.writeLog("OrganizationSyn-e:"+e);
            e.printStackTrace();
        }
        return body;
    }


    /**
     * 岗位同步
     * @param positionJson
     */
    public void doJobTitleSyn(JSONObject positionJson){
        BaseBean bb = new BaseBean();
        bb.writeLog("-----doJobTitleSyn-----");
        if(positionJson!=null)
        {
            RecordSet rs = new RecordSet();
            String jobtitlecode = positionJson.getString("positionCode");//岗位编码
            String jobtitlename = positionJson.getString("positionName");//岗位名称
            String deptCode = positionJson.getString("deptCode");//部门编码
            //bb.writeLog("岗位编码:"+jobtitlecode+",jobtitlename:"+jobtitlename+",部门编码:"+deptCode);
            if(!"".equals(deptCode) && !"".equals(jobtitlecode) && jobtitlecode !=null){
                String deptId = getDeptIdByCode(deptCode);
                //rs.writeLog("deptId:"+deptId);
                if(!"".equals(deptId))
                {
                    String jobTitlecId = getJobTitleIdByCode(jobtitlecode,deptId);
                    if(!"".equals(jobTitlecId) && jobTitlecId !=null)
                    {
                        if(!"".equals(jobtitlecode) && jobtitlecode !=null)
                        {
                            if("99999".equals(jobtitlecode)){
                                jobtitlename = "" ;
                            }
                            // rs.writeLog("11:");
                            String jobtitleUpateSql = " update hrmjobtitles set jobtitlemark='"+jobtitlename+"',jobtitlename='"+jobtitlename+"',jobdepartmentid='"+deptId+"' where id='"+jobTitlecId+"' ";
                            rs.executeUpdate(jobtitleUpateSql);
                        }
                    }else{
                        if(!"".equals(jobtitlecode) && jobtitlecode !=null)
                        {
                            if("99999".equals(jobtitlecode)){
                                jobtitlename = "" ;
                            }
                            //rs.writeLog("22:");
                            String jobtitleInsertSql = " insert into hrmjobtitles(jobtitlemark,jobtitlename,jobdepartmentid,jobtitlecode) values(?,?,?,?)";
                            rs.executeUpdate(jobtitleInsertSql,new Object[]{jobtitlename,jobtitlename,deptId,jobtitlecode});
                        }
                    }
                }
            }
        }
    }


    /***
     *
     * @param intresourceNum
     * @return
     */
    public String getUserIdByIntresourceNum(String intresourceNum){
        String userId = "";
        if(intresourceNum!=null && !"".equals(intresourceNum))
        {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmresource where id=? ",new Object[]{intresourceNum});
            if(rs.next()){
                userId = Util.null2String(rs.getString("id"));
            }
        }
        return userId;
    }


    /***
     *
     * @param intresourceNum
     * @return
     */
    public String getSecAccountIdByIntresourceNum(String intresourceNum){
        String userId = "";
        if(!"".equals(intresourceNum) && intresourceNum !=null)
        {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmresource where id=? and belongto is not null ",new Object[]{intresourceNum});
            if(rs.next()){
                userId = Util.null2String(rs.getString("id"));
            }
        }
        return userId;
    }



    /***
     *
     * @param userCode
     * @return
     */
    public String getUserIdByCode2Main(String userCode){
        String userId = "";
        if(userCode!=null && !"".equals(userCode)){
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmresource where workcode=? and belongto is null ",new Object[]{userCode});
            if(rs.next()){
                userId = Util.null2String(rs.getString("id"));
            }
        }
        return userId;
    }


    /***
     *
     * @param userCodeArray
     * @return
     */
    public String getUserIdsByCodes2Main(JSONArray userCodeArray){
        String userIds = "";
        if(userCodeArray!=null && userCodeArray.size()>0){
            for(int i=0;i<userCodeArray.size();i++)
            {
                String userId = getUserIdByCode2Main(userCodeArray.getString(i));
                if(!"".equals(userId))
                {
                    userIds += userIds== "" ? userId : ","+userId;
                }
            }
        }
        return userIds;
    }

    /***
     *
     * @param textfield2
     * @return
     */
    public String getSecAccountIdByTextfield2(String textfield2){
        String userId = "";
        if(!"".equals(textfield2) && textfield2 !=null)
        {
            RecordSet rs = new RecordSet();
            rs.executeQuery(" select id from hrmresource where textfield2=? ",new Object[]{textfield2});
            if(rs.next()){
                userId = Util.null2String(rs.getString("id"));
            }
        }
        return userId;
    }


    /***
     *
     */
    public void updatePositionStatus(){
        BaseBean bb = new BaseBean();
        RecordSet rs = new RecordSet();
        try{
            String sql = " select id,lastname,loginid,workcode,textfield2,textfield3 from hrmresource t\n" +
                    "  where t.belongto is not null\n" +
                    "  and t.status=1 \n" +
                    "  and t.textfield2 in(select textfield2 from hrmresource h where h.belongto is null and status=1) " ;
            rs.execute(sql);
            while (rs.next()){
                String id = Util.null2String(rs.getString("id"));
                String lastname = Util.null2String(rs.getString("lastname"));
                String loginid = Util.null2String(rs.getString("loginid"));
                String workcode = Util.null2String(rs.getString("id"));
                String textfield2 = Util.null2String(rs.getString("textfield2"));
                String textfield3 = Util.null2String(rs.getString("textfield3"));

                bb.writeLog("updatePositionStatus--id="+id+"--loginid="+loginid+"--workcode="+workcode+"--textfield2="+textfield2+"--textfield3="+textfield3);
            }

            sql = " update hrmresource set status=5 where id in(\n" +
                    "  select id from hrmresource t\n" +
                    "  where t.belongto is not null\n" +
                    "  and t.status=1\n" +
                    "  and t.textfield2 in(select textfield2 from hrmresource h where h.belongto is null and status=1)\n" +
                    ") " ;

            boolean bool = rs.executeUpdate(sql);
            bb.writeLog("updatePositionStatus--bool:"+bool);
        }catch (Exception e){

        }
    }


    /****
     *
     */
    public void updatePositionInfo()
    {
        BaseBean bb = new BaseBean();
        bb.writeLog("updatePositionInfo--");
        RecordSet rs = new RecordSet();
        try{

            String sql =" update hrmresource t\n" +
                        " set t.email=(select email from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.mobile=(select mobile from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.sex=(select sex from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.telephone=(select telephone from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.messagerurl=(select messagerurl from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.resourceimageid=(select resourceimageid from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.password=(select password from hrmresource l where l.workcode = t.workcode and l.belongto is null),\n" +
                        " t.creater=(select creater from hrmresource l where l.workcode = t.workcode and l.belongto is null)\n" +
                        " where t.belongto is not null\n" +
                        " and t.status=1 " +
                        " and (t.email is null or t.mobile is null or t.sex is null or t.telephone is null or t.messagerurl not like '/htoa/weaver%' or t.messagerurl is null or t.password is null or t.resourceimageid is null or t.creater is null) \n " ;

            boolean bool = rs.executeUpdate(sql);
            bb.writeLog("updatePositionStatus--bool:"+bool);
        }catch (Exception e){

        }
    }


    /***
     *
     * @param empCode
     * @param photo
     * @param userId
     */
    public void synHrmPhoto(String empCode,String photo,String userId){
        String rootpath = GCONST.getRootPath(); // "/app/ecology";
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("synHrmPhoto------empCode:"+empCode);
        // rs.writeLog("synHrmPhoto:empCode:"+empCode+"userId:"+userId);
        try{
            if(!"".equals(empCode) && empCode !=null)
            {
                // /app/ecology/messager/usericon/001553/001553.jpg
                // /htoa/messager/usericon/012901/012901.jpg

                String attachfilename = empCode+".jpg";
                String imagefiletype = "image/jpeg" ;
                String usericonpath = "filesystem/messager/usericon/"+ empCode;

                String usericonrealpath = rootpath+usericonpath;

                String usericonurl = "/htoa/"+usericonpath+"/"+attachfilename ;

                String usericonrealurl = rootpath+usericonpath+"/"+attachfilename ;

                File iconpath = new File(usericonrealpath);
                if (!iconpath.exists())
                {
                    iconpath.mkdirs();
                }
                if(!"".equals(photo) && photo !=null)
                {
                    boolean flag = base64ToImage(photo,usericonrealurl);
                    bb.writeLog("synHrmPhoto------empCode:"+empCode+" flag="+flag);
                    if(flag)
                    {
                        String resourceimageid = "" ;
                        String sql = " select resourceimageid from hrmresource where id="+userId ;
                        rs.execute(sql);
                        if(rs.next()){
                            resourceimageid = Util.null2String(rs.getString("resourceimageid"));
                        }

                        bb.writeLog("synHrmPhoto------empCode:"+empCode+" resourceimageid="+resourceimageid);
                        if(!"".equals(resourceimageid))
                        {
                            String imagefileidold = "" ;
                            sql = " select filerealpath,imagefileid from ImageFile where imagefileid="+resourceimageid;
                            rs.execute(sql);
                            if(rs.next()){
                                imagefileidold = Util.null2String(rs.getString("imagefileid"));
                            }
                            if(!"".equals(imagefileidold))
                            {
                                sql = " update ImageFile set filerealpath='"+usericonrealurl+"' where imagefileid ="+imagefileidold+"" ;
                                rs.writeLog("sql:"+sql);
                                rs.execute(sql);

                                if(!"".equals(userId))
                                {
                                    String fileid = "";
                                    try{
                                        fileid = DocDownloadCheckUtil.checkPermission(imagefileidold,null);
                                    }catch (Exception e){
                                        bb.writeLog("OrganizationSyn-e:"+e);
                                    }
                                    String fileurl = "/htoa/weaver/weaver.file.FileDownload?fileid="+fileid;
                                    sql = " update hrmresource set messagerurl='"+fileurl+"' where workcode='"+empCode+"'" ;
                                    rs.writeLog("sql:"+sql);
                                    rs.execute(sql);
                                }
                            }else{
                                ImageFileIdUpdate im = new ImageFileIdUpdate();
                                int imagefileid = im.getImageFileNewId();
                                rs.writeLog("imagefileid:"+imagefileid);
                                sql= " insert into ImageFile(imagefileid,imagefilename,imagefiletype,imagefileused,filerealpath,iszip,isencrypt,fileSize) "+
                                        " values("+imagefileid+",'"+attachfilename+"','"+imagefiletype+"',1,'"+usericonrealurl+"',0,0,'"+photo.length()+"')";
                                rs.execute(sql);

                                if(!"".equals(userId))
                                {
                                    String fileid = "";
                                    try{
                                        fileid = DocDownloadCheckUtil.checkPermission(imagefileid+"",null);
                                    }catch (Exception e){
                                        bb.writeLog("OrganizationSyn-e:"+e);
                                    }

                                    String fileurl = "/htoa/weaver/weaver.file.FileDownload?fileid="+fileid;
                                    sql = " update hrmresource set resourceimageid="+imagefileid+",messagerurl='"+fileurl+"' where workcode='"+empCode+"'" ;
                                    rs.writeLog("sql:"+sql);
                                    rs.execute(sql);
                                }
                            }
                        }else{
                            ImageFileIdUpdate im = new ImageFileIdUpdate();
                            int imagefileid = im.getImageFileNewId();
                            rs.writeLog("imagefileid:"+imagefileid);
                            rs.writeLog("imagefileid:"+imagefileid);
                            sql= " insert into ImageFile(imagefileid,imagefilename,imagefiletype,imagefileused,filerealpath,iszip,isencrypt,fileSize) "+
                                    " values("+imagefileid+",'"+attachfilename+"','"+imagefiletype+"',1,'"+usericonrealurl+"',0,0,'"+photo.length()+"')";
                            rs.writeLog("sql:"+sql);
                            rs.execute(sql);

                            if(!"".equals(userId))
                            {
                                String fileid = "";
                                try{
                                    fileid = DocDownloadCheckUtil.checkPermission(imagefileid+"",null);
                                }catch (Exception e){
                                    bb.writeLog("OrganizationSyn-e:"+e);
                                }

                                String fileurl = "/htoa/weaver/weaver.file.FileDownload?fileid="+fileid;
                                sql = " update hrmresource set resourceimageid="+imagefileid+",messagerurl='"+fileurl+"' where workcode='"+empCode+"'" ;
                                rs.writeLog("sql:"+sql);
                                rs.execute(sql);
                            }
                        }
                    }else{
                        bb.writeLog(empCode+": no Photo22222");
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            bb.writeLog("OrganizationSyn-e:"+e);
        }
    }


    /**
     * @Descriptionmap 对字节数组字符串进行Base64解码并生成图片
     * @author temdy
     * @Date 2015-01-26
     * @param base64 图片Base64数据
     * @param path 图片路径
     * @return
     */
    public boolean base64ToImage(String base64,String path) {// 对字节数组字符串进行Base64解码并生成图片
        if (base64 == null) { // 图像数据为空
            return false;
        }
        BaseBean bb = new BaseBean();
        bb.writeLog("path:"+path);
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] bytes = decoder.decodeBuffer(base64);
            for (int i = 0; i < bytes.length; ++i) {
                if (bytes[i] < 0) {// 调整异常数据
                    bytes[i] += 256;
                }
            }
            // 生成jpeg图片
            OutputStream out = new FileOutputStream(path);
            out.write(bytes);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            bb.writeLog("OrganizationSyn-e:"+e);
            return false;
        }
    }


}
